import os
import re
from tqdm import tqdm
import json
from control.models import User
from unit_management.models import *
from basic_component_related_management.models import *

# 初始化一个superadmin的账号，密码为superadmin1234


def init_superadmin():
    user_entity = User.objects.filter(user_account="superadmin").first()
    if user_entity is None:
        user_entity = User.objects.create(
            user_account="superadmin",
            user_name="superadmin",
            password="C6IDQGUT42vVUX0neDCF2TB2kD42tBWwp4AGqp3DenmkTYI9urFbhJomuOMcI25De98AjQJPBT07AUW5oQdYeHfeQKr/uhR6OABnKu+/uof5viBguKPdDqkc/znPMjADdt66dxVi/yeur8EahDaUCvhssdB4HkJLxteCLGlEDZ/AJe1BiTBCgyN+7ZLCEjxXD4lKbI5mT9oWEQ+Pd1rE7fY/jmuDh6Nnz2YO7/0WvQA39KESBVzrVQ5bmEVSnUNkQqfzR8DkylHQ54swmNGXFog9xd7q0gjd2VrsBfE6qD4y+iJThQlALLh1f9QdVthAcuepLUKW+PI/mmyllQTufw=="
        )
    return user_entity

# 初始化单位部分


def init_unit(user_entity):
    file_path = "init_code/little_init_component_unit/unit_data.json"
    with open(file_path, 'r', encoding="utf-8") as f:
        data = json.load(f)
    # 初始化前缀
    unit_prefix_data = data["prefix"]
    unit_prefix_list = []
    for unit_prefix in unit_prefix_data:
        unit_prefix_1 = UnitPrefix(
            prefix_name=unit_prefix["prefix_name"],
            prefix_name_en=unit_prefix["prefix_name_en"],
            prefix_base=unit_prefix["prefix_base"],
            prefix_power=unit_prefix["prefix_power"],
            prefix_symbol=unit_prefix["prefix_symbol"],
            prefix_symbol_ascii=unit_prefix["prefix_symbol_ascii"],
            create_person=user_entity,
            modify_person=user_entity
        )
        unit_prefix_list.append(unit_prefix_1)
    UnitPrefix.objects.bulk_create(unit_prefix_list)
    # 初始化基本量纲
    unit_basic_dimension_data = data["basic_dimension"]
    unit_basic_dimension_list = []
    unit_basic_dimension_symbol_entity_map = {}
    for unit_basic_dimemsion in unit_basic_dimension_data:
        basic_dimension1 = BasicDimension(
            basic_dimension_name=unit_basic_dimemsion["basic_dimension_name"],
            basic_dimension_name_en=unit_basic_dimemsion["basic_dimension_name_en"],
            basic_dimension_symbol=unit_basic_dimemsion["basic_dimension_symbol"],
            basic_dimension_symbol_ascii=unit_basic_dimemsion["basic_dimension_symbol_ascii"],
            create_person=user_entity,
            modify_person=user_entity
        )
        unit_basic_dimension_list.append(basic_dimension1)
        unit_basic_dimension_symbol_entity_map[unit_basic_dimemsion["basic_dimension_symbol"]] = basic_dimension1
    BasicDimension.objects.bulk_create(unit_basic_dimension_list)

    # 初始化量纲
    unit_dimension_data = data["dimension"]
    unit_dimension_list = []
    unit_dimension_name_entity_map = {}
    unit_dimension_consistent_entity_list = []
    for unit_dimension in unit_dimension_data:
        dimension1 = Dimension(
            dimension_name=unit_dimension["dimension_name"],
            dimension_name_en=unit_dimension["dimension_name_en"],
            create_person=user_entity,
            modify_person=user_entity
        )
        unit_dimension_list.append(dimension1)
        for basic_dimension_symbol in unit_dimension["dimension_composition"]:
            dimension_map1 = DimensionMap(
                basic_dimension=unit_basic_dimension_symbol_entity_map[basic_dimension_symbol],
                dimension=dimension1,
                power=unit_dimension["dimension_composition"][basic_dimension_symbol]
            )
            unit_dimension_consistent_entity_list.append(dimension_map1)
        unit_dimension_name_entity_map[unit_dimension["dimension_name"]] = dimension1
    Dimension.objects.bulk_create(unit_dimension_list)
    DimensionMap.objects.bulk_create(unit_dimension_consistent_entity_list)

    # 初始化单位
    unit_data = data["unit"]
    unit_list = []
    unit_symbol_entity_map = {}
    for unit in unit_data:
        unit1 = Unit(
            unit_name=unit["unit_name"],
            unit_name_en=unit["unit_name_en"],
            unit_symbol=unit["unit_symbol"],
            dimension=unit_dimension_name_entity_map[unit["dimension_name"]],
            is_show=unit["is_show"],
            create_person=user_entity,
            modify_person=user_entity
        )
        if unit["root_unit"] != None:
            unit1.root_unit = unit_symbol_entity_map[unit["root_unit"]]
            unit1.conversion_factor = unit["conversion_factor"]
            unit1.conversion_offset = unit["conversion_offset"]
        unit_list.append(unit1)
        unit_symbol_entity_map[unit["unit_symbol"]] = unit1
    Unit.objects.bulk_create(unit_list)

    # 初始化量
    quantity_data = data["quantity"]
    quantity_list = []
    quantity_unit_list = []
    for quantity in quantity_data:
        quantity1 = Quantity(
            quantity_name=quantity["quantity_name"],
            quantity_name_en=quantity["quantity_name_en"],
            quantity_symbol=quantity["quantity_symbol"],
            quantity_type=quantity["quantity_type"],
            dimension=unit_dimension_name_entity_map[quantity["dimension_name"]],
            create_person=user_entity,
            modify_person=user_entity
        )
        quantity_list.append(quantity1)
        for unit_choice in quantity["unit_choice"]:
            quantity_unit_map1 = QuantityUnitMap(
                quantity=quantity1,
                unit=unit_symbol_entity_map[unit_choice]
            )
            quantity_unit_list.append(quantity_unit_map1)
    Quantity.objects.bulk_create(quantity_list)
    QuantityUnitMap.objects.bulk_create(quantity_unit_list)

    print("单位初始化完成")


def init_component(user_entity):
    file_path = "init_code/little_init_component_unit/component_data.json"
    with open(file_path, 'r', encoding="utf-8") as f:
        data = json.load(f)

    # 初始化组分类别
    component_group_data = data["component_group"]
    component_group_list = []
    for component_group in component_group_data:
        component_group1 = ComponentGroup(
            name=component_group["name"],
            name_en=component_group["name_en"],
            create_person=user_entity,
            modify_person=user_entity
        )
        component_group_list.append(component_group1)
    ComponentGroup.objects.bulk_create(component_group_list)

    # 初始化组分属性
    component_attribute_data = data["attribute"]
    component_attribute_list = []
    for component_attribute in component_attribute_data:
        component_attribute1 = Attribute(
            name=component_attribute["name"],
            name_en=component_attribute["name_en"],
            data_type=component_attribute["data_type"],
            necessary=component_attribute["necessary"],
            multiple_value_used=component_attribute["multiple_value_used"],
            common_used=component_attribute["common_used"],
            create_person=user_entity,
            modify_person=user_entity
        )
        component_attribute_list.append(component_attribute1)
    Attribute.objects.bulk_create(component_attribute_list)

    # 初始化应用物性
    const_property_data = data["const_property"]
    const_property_list = []
    for const_property in const_property_data:
        const_property1 = ComponentConstProperty(
            name=const_property["name"],
            name_en=const_property["name_en"],
            quantity=Quantity.objects.filter(
                quantity_name=const_property["quantity_name"]).first(),
            create_person=user_entity,
            modify_person=user_entity
        )
        const_property_list.append(const_property1)
    ComponentConstProperty.objects.bulk_create(const_property_list)

    # 初始化组分
    component_data = data["component"]
    component_list = []
    component_attribute_value_list = []
    component_const_property_value_list = []
    component_group_map_list = []
    for component in component_data:
        component1 = ComponentIdentifier(
            name=component["name"],
            name_en=component["name_en"],
            molecular_formula=component["molecular_formula"],
            cas_number=component["cas_number"],
            smiles_number=component["smiles_number"],
            create_person=user_entity,
            modify_person=user_entity
        )
        component_list.append(component1)
        for component_group in component["component_group"]:
            component_group_map1 = ComponentIdeComponentGroupMap(
                componentidentifier=component1,
                componentGroup=ComponentGroup.objects.filter(
                    name=component_group).first()
            )
            component_group_map_list.append(component_group_map1)
        for component_attribute in component["attribute"]:
            component_attribute_value1 = ComponentAttributeValue(
                componentidentifier=component1,
                attribute=Attribute.objects.filter(
                    name=component_attribute["attribute_name"]).first(),
                attribute_value=component_attribute["attribute_value"]
            )
            component_attribute_value_list.append(component_attribute_value1)
        for component_const_property in component["const_property"]:
            component_const_property_value1 = ComponentConstPropertyValue(
                component=component1,
                property=ComponentConstProperty.objects.filter(
                    name=component_const_property["const_property_name"]).first(),
                property_value=component_const_property["const_property_value"],
                property_unit=Unit.objects.filter(
                    unit_symbol=component_const_property["unit"]).first()
            )
            component_const_property_value_list.append(
                component_const_property_value1)
    ComponentIdentifier.objects.bulk_create(component_list)
    ComponentAttributeValue.objects.bulk_create(component_attribute_value_list)
    ComponentConstPropertyValue.objects.bulk_create(
        component_const_property_value_list)
    ComponentIdeComponentGroupMap.objects.bulk_create(component_group_map_list)
    print("组分初始化完成")


def init_property(user_entity):
    file_path = "init_code/little_init_component_unit/property_data.json"
    with open(file_path, 'r', encoding="utf-8") as f:
        data = json.load(f)
    property_group_data = data["property_group"]
    property_group_list = []
    property_list = []
    for property_group in property_group_data:
        property_group1 = PropertyGroup(
            name=property_group["name"],
            name_en=property_group["name_en"],
            is_pure_used=property_group["is_pure_used"],
            is_binary_used=property_group["is_binary_used"],
            is_ternary_used=property_group["is_ternary_used"],
            is_multi_used=property_group["is_multi_used"],
            is_reaction_used=property_group["is_reaction_used"],
            is_complex_used=property_group["is_complex_used"],
            create_person=user_entity,
            modify_person=user_entity
        )
        property_group_list.append(property_group1)
        for property in property_group["property_consistent"]:
            property1 = Property(
                name=property["name"],
                name_en=property["name_en"],
                property_group=property_group1,
                quantity=Quantity.objects.filter(
                    quantity_name=property["quantity"]).first(),
                is_single_value=property["is_single_value"],
                is_fixed_value=property["is_fixed_value"],
                is_one_variable=property["is_one_variable"],
                is_two_variable=property["is_two_variable"],
                is_multi_variable=property["is_multi_variable"],
                is_mixz=property["is_mixz"],
                is_mixv=property["is_mixv"],
                is_reaction=property["is_reaction"],
                is_need_solvent_binary=property["is_need_solvent_binary"],
                is_need_solvent_ternary=property["is_need_solvent_ternary"],
                create_person=user_entity,
                modify_person=user_entity
            )
            property_list.append(property1)
    PropertyGroup.objects.bulk_create(property_group_list)
    Property.objects.bulk_create(property_list)

    variable_data = data["variable"]
    variable_list = []
    property_variable_list = []
    for variable in variable_data:
        variable1 = PropertyVariable(
            name=variable["name"],
            name_en=variable["name_en"],
            quantity=Quantity.objects.filter(
                quantity_name=variable["quantity"]).first(),
            pure_used=variable["pure_used"],
            binary_used=variable["binary_used"],
            ternary_used=variable["ternary_used"],
            multi_used=variable["multi_used"],
            constrain_used=variable["constrain_used"],
            component_related=variable["component_related"],
            need_solvent_binary=variable["need_solvent_binary"],
            need_solvent_ternary=variable["need_solvent_ternary"],
            create_person=user_entity,
            modify_person=user_entity
        )
        variable_list.append(variable1)
        for property in variable["property_used"]:
            property_variable1 = PropertyPropertyVariableMap(
                property=Property.objects.filter(
                    name=property).first(),
                property_variable=variable1
            )
            property_variable_list.append(property_variable1)
    PropertyVariable.objects.bulk_create(variable_list)
    PropertyPropertyVariableMap.objects.bulk_create(property_variable_list)

    experimental_method_data = data["experimentalMethod"]
    experimental_method_list = []
    property_experimental_method_list = []
    for experimental_method in experimental_method_data:
        experimental_method1 = Method(
            name=experimental_method["name"],
            name_en=experimental_method["name_en"],
            create_person=user_entity,
            modify_person=user_entity
        )
        experimental_method_list.append(experimental_method1)
        for property in experimental_method["property_used"]:
            property_experimental_method1 = MethodPropertyMap(
                property=Property.objects.filter(
                    name=property).first(),
                method=experimental_method1
            )
            property_experimental_method_list.append(
                property_experimental_method1)
    Method.objects.bulk_create(experimental_method_list)
    MethodPropertyMap.objects.bulk_create(property_experimental_method_list)

    phase_data = data["phase"]
    phase_list = []
    for phase in phase_data:
        phase1 = Phase(
            name=phase["name"],
            name_en=phase["name_en"],
            is_pure_used=phase["is_pure_used"],
            is_binary_used=phase["is_binary_used"],
            is_ternary_used=phase["is_ternary_used"],
            is_multi_used=phase["is_multi_used"],
            is_reaction_used=phase["is_reaction_used"],
            is_complex_used=phase["is_complex_used"],
            create_person=user_entity,
            modify_person=user_entity
        )
        phase_list.append(phase1)
    Phase.objects.bulk_create(phase_list)

    print("物性初始化完成")


def init_other(user_entity):
    file_path = "init_code/little_init_component_unit/other_data.json"
    with open(file_path, 'r', encoding="utf-8") as f:
        data = json.load(f)

    purification_data = data["Purification"]
    purification_list = []
    for purification in purification_data:
        purification1 = Purification(
            name=purification["name"],
            name_en=purification["name_en"],
            create_person=user_entity,
            modify_person=user_entity
        )
        purification_list.append(purification1)

    Purification.objects.bulk_create(purification_list)

    purityDetermination_data = data["PurityDetermination"]
    purityDetermination_list = []
    for purityDetermination in purityDetermination_data:
        purityDetermination1 = PurityDetermination(
            name=purityDetermination["name"],
            name_en=purityDetermination["name_en"],
            create_person=user_entity,
            modify_person=user_entity
        )
        purityDetermination_list.append(purityDetermination1)

    PurityDetermination.objects.bulk_create(purityDetermination_list)

    print("其他初始化完成")


def init_data():
    user_entity = init_superadmin()
    init_unit(user_entity)
    init_component(user_entity)
    init_property(user_entity)
    # init_other(user_entity)
